using Microsoft.Phone.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Microsoft.Phone.Controls
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(PivotItem)), TemplatePart(Name = "HeadersListElement", Type = typeof(PivotHeadersControl)), TemplatePart(Name = "PivotItemPresenter", Type = typeof(ItemsPresenter))]
	public class LockablePivot : Pivot
	{
		private struct HeaderAnimationInfo
		{
			public double opacity;
			public double originalX;
			public OpacityAnimator opacityAnimator;
			public TransformAnimator transformAnimator;
			public TranslateTransform transform;
		}
		private const string HeadersListElement = "HeadersListElement";
		private const string PivotItemPresenterElement = "PivotItemPresenter";
		internal const string ItemContainerStyleName = "ItemContainerStyle";
		private const double _animOffset = 20.0;
		private bool _isLocked;
		private bool _isUpdating;
		public static readonly DependencyProperty IsLockedProperty = DependencyProperty.Register("IsLocked", typeof(bool), typeof(LockablePivot), new PropertyMetadata(false, new PropertyChangedCallback(LockablePivot.OnIsLockedPropertyChanged)));
		private PivotItem[] _savedItems;
		private LockablePivot.HeaderAnimationInfo[] _animInfo;
		private PivotHeadersControl _header;
		private int _savedIndex;
		private static Duration _animTime = new Duration(TimeSpan.FromMilliseconds(200.0));
		public bool IsLocked
		{
			get
			{
				return (bool)base.GetValue(LockablePivot.IsLockedProperty);
			}
			set
			{
				base.SetValue(LockablePivot.IsLockedProperty, value);
			}
		}
		private static void OnIsLockedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			LockablePivot lockablePivot = d as LockablePivot;
			if (lockablePivot != null)
			{
				lockablePivot.OnIsLockedChanged((bool)e.get_NewValue());
			}
		}
		public LockablePivot()
		{
			base.SelectionChanged += new SelectionChangedEventHandler(this.OnSelectionChanged);
		}
		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
		{
			if (!this._isUpdating)
			{
				if (this._isLocked)
				{
					throw new InvalidOperationException("Pivot Items cannot be modified when locked");
				}
				this._animInfo = null;
				base.OnItemsChanged(e);
			}
		}
		private PivotHeadersControl FindHeader(UIElement start)
		{
			UIElement uIElement = null;
			int childrenCount = VisualTreeHelper.GetChildrenCount(start);
			for (int i = 0; i < childrenCount; i++)
			{
				UIElement uIElement2 = VisualTreeHelper.GetChild(start, i) as UIElement;
				if (uIElement2 is PivotHeadersControl)
				{
					uIElement = uIElement2;
				}
				else
				{
					uIElement = this.FindHeader(uIElement2);
				}
				if (uIElement != null)
				{
					break;
				}
			}
			return uIElement as PivotHeadersControl;
		}
		private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			this._animInfo = null;
		}
		private void OnIsLockedChanged(bool newValue)
		{
			this._isLocked = newValue;
			this._isUpdating = true;
			if (this._isLocked)
			{
				this._savedIndex = base.SelectedIndex;
				this.FadeOutHeaders();
				this.SaveAndRemoveItems();
			}
			else
			{
				this.RestoreItems();
				this.FadeInHeaders();
			}
			this._isUpdating = false;
		}
		private void CreateAnimationInformation()
		{
			if (this._animInfo == null)
			{
				int count = this._header.get_Items().get_Count();
				this._animInfo = new LockablePivot.HeaderAnimationInfo[count - 1];
				int num = 0;
				using (IEnumerator<object> enumerator = this._header.get_Items().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						PivotHeaderItem pivotHeaderItem = (PivotHeaderItem)enumerator.get_Current();
						if (!pivotHeaderItem.IsSelected)
						{
							this._animInfo[num].opacity = pivotHeaderItem.get_Opacity();
							this._animInfo[num].opacityAnimator = new OpacityAnimator(pivotHeaderItem);
							this._animInfo[num].transform = TransformAnimator.GetTranslateTransform(pivotHeaderItem);
							this._animInfo[num].transformAnimator = new TransformAnimator(this._animInfo[num].transform);
							this._animInfo[num].originalX = this._animInfo[num].transform.get_X();
							num++;
						}
					}
				}
			}
		}
		private void SaveAndRemoveItems()
		{
			this._savedItems = new PivotItem[base.get_Items().get_Count()];
			base.get_Items().CopyTo(this._savedItems, 0);
			for (int i = base.get_Items().get_Count() - 1; i > this._savedIndex; i--)
			{
				base.get_Items().RemoveAt(i);
			}
			for (int j = 0; j < this._savedIndex; j++)
			{
				base.get_Items().RemoveAt(0);
			}
		}
		private void RestoreItems()
		{
			for (int i = 0; i < this._savedIndex; i++)
			{
				base.get_Items().Insert(i, this._savedItems[i]);
			}
			for (int j = this._savedIndex + 1; j < this._savedItems.Length; j++)
			{
				base.get_Items().Add(this._savedItems[j]);
			}
			this._savedItems = null;
		}
		private void FadeOutHeaders()
		{
			if (this._header == null)
			{
				this._header = this.FindHeader(this);
			}
			this.CreateAnimationInformation();
			LockablePivot.HeaderAnimationInfo[] animInfo = this._animInfo;
			for (int i = 0; i < animInfo.Length; i++)
			{
				LockablePivot.HeaderAnimationInfo headerAnimationInfo = animInfo[i];
				headerAnimationInfo.opacityAnimator.GoTo(0.0, LockablePivot._animTime);
				headerAnimationInfo.transformAnimator.GoTo(20.0 + headerAnimationInfo.originalX, LockablePivot._animTime);
			}
		}
		private void FadeInHeaders()
		{
			LockablePivot.HeaderAnimationInfo[] animInfo = this._animInfo;
			for (int i = 0; i < animInfo.Length; i++)
			{
				LockablePivot.HeaderAnimationInfo headerAnimationInfo = animInfo[i];
				headerAnimationInfo.opacityAnimator.GoTo(headerAnimationInfo.opacity, LockablePivot._animTime);
				headerAnimationInfo.transformAnimator.GoTo(headerAnimationInfo.originalX, LockablePivot._animTime);
			}
		}
	}
}
